using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class manager_news_list_news : System.Web.UI.Page
{
    #region variables

    string user_present = string.Empty;
    string gvUniqueID = string.Empty;
    int gvNewPageIndex = 0;
    int gvEditIndex = -1;
    string gvSortExpr = string.Empty;

    private string Order
    {

        get { return ViewState["SortDirection"] as string ?? "ASC"; }

        set { ViewState["SortDirection"] = value; }

    }

    #endregion
    //This procedure returns the Sort Direction
    private string GetSortDirection()
    {
        switch (Order)
        {
            case "ASC":
                Order = "DESC";
                break;

            case "DESC":
                Order = "ASC";
                break;
        }
        return Order;
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        user_present = (string)Session["user"];
        if (user_present == null)
        {
            Response.Redirect("../main/login.aspx");
        }
        if (Page.IsPostBack == false)
        {
            try
            {
                if (Request["action"] == "search")
                {
                    panel_search.Visible = true;
                    panel_content.Visible = false;
                }
                else
                {
                    panel_content.Visible = true;
                    panel_search.Visible = false;
                    BindData();
                }
            }
            catch (Exception ex)
            {
                panel_content.Visible = true;
                panel_search.Visible = false;
                BindData();
            }

        }
    }

    protected void BindData()
    {    

        if (gvSortExpr != string.Empty)
        {
            GetSortDirection();
            GridView1.DataSource = News.get_news_and_sort(gvSortExpr, Order);
        }
        else
        {
            GridView1.DataSource = News.get_news_and_sort("UpdatedDate", "DESC"); ;
        }

        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row;
        string strSort = string.Empty;
        // Make sure we aren't in header/footer rows
        if (row.DataItem == null)
            return;
        GridView gv = new GridView();

        //Add delete confirmation message for Customer
        LinkButton l = (LinkButton)e.Row.FindControl("link_delete");
        l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete this news: " + DataBinder.Eval(e.Row.DataItem, "NewsTitle") + " ')");
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //Check if Add button clicked
        //if (e.CommandName == "add_user")
        //{
        //    try
        //    {
        //        //get values stored in the textbox
        //        string strUserName = ((TextBox)GridView1.FooterRow.FindControl("text_username")).Text;
        //        string strEmail = ((TextBox)GridView1.FooterRow.FindControl("text_email")).Text;
        //        string strFirstName = ((TextBox)GridView1.FooterRow.FindControl("text_firstname")).Text;
        //        string strLastName = ((TextBox)GridView1.FooterRow.FindControl("text_lastname")).Text;
        //        string strMobiPhone = ((TextBox)GridView1.FooterRow.FindControl("text_mobiphone")).Text;
        //        Users.create_user(strUserName,"123",strEmail,strFirstName,strLastName,strMobiPhone,true,user_present);
        //        BindData();
        //    }
        //    catch (Exception ex)
        //    { 
        //        ClientScript.RegisterStartupScript(GetType(),"Message","<script language='javascript' >alert('"+ex.Message.ToString().Replace("'","") +"');</script>");
        //    }
        //}

    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //int userid = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("label_userid")).Text);
        //string strUserName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("text_username")).Text;
        //string strEmail = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("text_email")).Text;
        //string strFirstName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("text_firstname")).Text;
        //string strLastName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("text_lastname")).Text;
        //string strMobiPhone = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("text_mobiphone")).Text;
        //Users.update_user(userid, strUserName, "123", strEmail, strFirstName, strLastName, strMobiPhone, true, user_present);
        //BindData();
        //ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('User updated successfully');</script>");
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        int newsid = Convert.ToInt32(((Label)GridView1.Rows[e.NewEditIndex].FindControl("label_newsid")).Text);

        //GridView gvTemp = (GridView)sender;
        //gvUniqueID = gvTemp.UniqueID;
        //gvEditIndex = e.NewEditIndex;
        //GridView1.DataBind();

        Response.Redirect("view_news.aspx?id=" + newsid);
    }

    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        //Check if there is any exception while updating
        if (e.Exception != null)
        {
            ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + e.Exception.Message.ToString().Replace("'", "") + "');</script>");
            e.ExceptionHandled = true;
        }

    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        int newsID = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("label_newsid")).Text);
        News.delete_news(newsID);
        BindData();
    }

    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        //Check if there is any exception while deleting
        if (e.Exception != null)
        {
            ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + e.Exception.Message.ToString().Replace("'", "") + "');</script>");
            e.ExceptionHandled = true;
        }
    }

    protected void GridView1_Sorted(object sender, EventArgs e)
    {

    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        GridView gvTemp = (GridView)sender;
        gvUniqueID = gvTemp.UniqueID;
        gvSortExpr = e.SortExpression;
        BindData();
    }


    //execute when checkbox selected or deselected
    protected void checkbox_changed(object sender, EventArgs e)
    {
        CheckBox checkbox = (CheckBox)sender;
        GridViewRow row = (GridViewRow)checkbox.NamingContainer;
        int newsid = Convert.ToInt32(((Label)GridView1.Rows[row.RowIndex].FindControl("label_newsid")).Text);
        News.update_news(newsid, checkbox.Checked, user_present);
    }

    protected void message_box(string messgage)
    {
        ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + messgage + "');</script>");
    }


    protected void goto_search(object sender, EventArgs e)
    {
        panel_content.Visible = true;

        GridView1.DataSource = News.search_news(RadioButtonList1.SelectedValue, text_search.Text, News.Anywhere);
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView gvTemp = (GridView)sender;
        gvUniqueID = gvTemp.UniqueID;
        gvNewPageIndex = e.NewPageIndex;
        GridView1.PageIndex = e.NewPageIndex;
        BindData();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView gvTemp = (GridView)sender;
        gvUniqueID = gvTemp.UniqueID;
        gvEditIndex = -1;
        GridView1.DataBind();
    }
}
